package com.liusand.user.controller;

import com.liusand.entity.dto.PhoneAndVerifyCodeDto;
import com.liusand.entity.dto.SettingDto;
import com.liusand.entity.dto.TwoPhoneDto;
import com.liusand.entity.pojo.Setting;
import com.liusand.entity.vo.Result;
import com.liusand.user.service.SettingService;
import com.liusand.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author :liusha
 * packageName :com.liusand.user.controller
 * fileName :SettingController
 * creationTime :2022/12/20 16:21
 */
@RestController
@RequestMapping("/user/setting")
public class SettingController {

    @Autowired
    private SettingService settingService;

    @Autowired
    private UserService userService;

    @PutMapping
    public Result modifySetting(@RequestBody SettingDto settingDto){
        boolean result = settingService.modifySetting(settingDto);
        if(!result){
            return Result.error("修改失败");
        }
        return Result.success();
    }

    @GetMapping
    public Result findSetting(){
        Setting setting = settingService.findSetting();
        return Result.success(setting);
    }

    @PostMapping("/verifyPhone")
    public Result verifyPhone(@RequestBody PhoneAndVerifyCodeDto phoneAndCode){
        boolean result = userService.verfyPhone(phoneAndCode);
        if(!result){
            return Result.error("验证失败");
        }
        return Result.success();
    }


    @PostMapping("updatePhone")
    public Result updatePhone(@RequestBody @Validated TwoPhoneDto twoPhoneDto){
        if(twoPhoneDto.getNewPhone().equals(twoPhoneDto.getOldPhone())){
            return Result.error("修改失败");
        }
        boolean result = userService.updatePhone(twoPhoneDto);
        if(!result){
            return Result.error("修改失败");
        }
        return Result.success();
    }
}
